{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cc7674a9-6164-4424-85a9-f669454cfd2a",
   "metadata": {},
   "source": [
    "I used this project to play about with Gradio blocks a little bit as it had more inputs than the other projects I've done.\n",
    "Its a password generator which I have no doubt I will use!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04c8d2dd-cb9a-4b18-b12d-48ed2f39679a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# imports\n",
    "\n",
    "import os\n",
    "from dotenv import load_dotenv\n",
    "from openai import OpenAI\n",
    "import requests\n",
    "import google.generativeai\n",
    "import anthropic\n",
    "from IPython.display import Markdown, display, update_display\n",
    "import gradio as gr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04521351-f220-42fe-9dc5-d0be80c95dd7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# keys\n",
    "\n",
    "load_dotenv(override=True)\n",
    "openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "if openai_api_key:\n",
    "    print(\"All good\")\n",
    "else:\n",
    "    print(\"OpenAI key issue\")\n",
    "\n",
    "claude_api_key = os.getenv(\"ANTHROPIC_API_KEY\")\n",
    "\n",
    "if claude_api_key:\n",
    "    print(\"All good\")\n",
    "else:\n",
    "    print(\"Claude key issue\")\n",
    "\n",
    "google_api_key = os.getenv(\"GOOGLE_API_KEY\")\n",
    "\n",
    "if google_api_key:\n",
    "    print(\"All good\")\n",
    "else:\n",
    "    print(\"Google key issue\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70fd3748-e6b6-4ac2-89a5-ef65ed7e41a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# initialise\n",
    "\n",
    "openai = OpenAI()\n",
    "claude = anthropic.Anthropic()\n",
    "google.generativeai.configure()\n",
    "\n",
    "OPENAI_MODEL = \"gpt-4o\"\n",
    "CLAUDE_MODEL = \"claude-sonnet-4-20250514\"\n",
    "GOOGLE_MODEL = \"gemini-2.0-flash\"\n",
    "\n",
    "max_tok = 500"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a448651-e426-4c3c-96f7-d69975dc7b10",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Prompts\n",
    "\n",
    "def pass_system_prompt(required_len, spec_char=\"Y\",num_char=\"Y\",min_lowercase=1,min_uppercase=1):\n",
    "\n",
    "    system_prompt = f\"\"\"You are a secure password generator. Your task is to create a single, cryptographically strong password that meets ALL specified requirements.\n",
    "    \n",
    "CRITICAL REQUIREMENTS:\n",
    "- Length: EXACTLY {required_len} characters\n",
    "- Must include: At least {min_lowercase} lowercase letter(s) AND at least {min_uppercase} uppercase letter(s)\n",
    "- Special characters: {'REQUIRED - include at least 1 char' if spec_char else 'FORBIDDEN - do not include any'}\n",
    "- Numbers: {'REQUIRED - include at least 1 digit' if num_char else 'FORBIDDEN - do not include any digits'}\n",
    "\n",
    "SECURITY RULES:\n",
    "1. Generate truly random passwords - avoid patterns, dictionary words, or predictable sequences\n",
    "2. Distribute character types evenly throughout the password\n",
    "3. Do not use repeated characters excessively (max 2 of same character)\n",
    "4. Ensure password meets minimum complexity for each required character type\n",
    "\n",
    "OUTPUT FORMAT:\n",
    "- Respond with ONLY the generated password\n",
    "- No explanations, no additional text, just the password\n",
    "- Verify the password meets ALL requirements before responding\"\"\"\n",
    "\n",
    "    return system_prompt\n",
    "\n",
    "def pass_user_prompt(required_len, spec_char=\"Y\",num_char=\"Y\",min_lowercase=1,min_uppercase=1):\n",
    "    \n",
    "    user_prompt = f\"\"\"Generate a secure password with these exact specifications:\n",
    "    \n",
    "Length: {required_len} characters\n",
    "Lowercase letters: Required (minimum {min_lowercase})\n",
    "Uppercase letters: Required (minimum {min_uppercase})\n",
    "Numbers: {'Required (minimum 1)' if num_char else 'Not allowed'}\n",
    "Special characters: {'Required (minimum 1)' if spec_char else 'Not allowed'}\n",
    "\n",
    "Requirements verification checklist:\n",
    "✓ Exactly {required_len} characters total\n",
    "✓ Contains {min_lowercase}+ lowercase letters\n",
    "✓ Contains {min_uppercase}+ uppercase letters\n",
    "✓ {'Contains 1+ numbers' if num_char else 'Contains NO numbers'}\n",
    "✓ {'Contains 1+ special characters' if spec_char else 'Contains NO special characters'}\n",
    "✓ No obvious patterns or dictionary words\n",
    "✓ Good distribution of character types\n",
    "\n",
    "Generate the password now.\"\"\"\n",
    "\n",
    "    return user_prompt\n",
    "        \n",
    "def pass_messages(required_len, spec_char,num_char,min_lowercase,min_uppercase):\n",
    "    messages = [\n",
    "        {\"role\":\"system\",\"content\":pass_system_prompt(required_len, spec_char,num_char,min_lowercase,min_uppercase)},\n",
    "        {\"role\":\"user\",\"content\":pass_user_prompt(required_len, spec_char,num_char,min_lowercase,min_uppercase)}\n",
    "    ]\n",
    "\n",
    "    return messages\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "857370b0-35a5-4b50-8715-86f8e781523b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#test\n",
    "\n",
    "messages1 = pass_messages(12, \"N\", \"Y\",1,1)\n",
    "print(messages1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59ab4279-90a8-4997-8e15-f07295856222",
   "metadata": {},
   "outputs": [],
   "source": [
    "def openai_password_gen(required_len, spec_char, num_char,min_lowercase,min_uppercase):\n",
    "    response=openai.chat.completions.create(\n",
    "        model=OPENAI_MODEL,\n",
    "        max_tokens=max_tok,\n",
    "        messages=pass_messages(required_len, spec_char,num_char,min_lowercase,min_uppercase)\n",
    "    )\n",
    "    return response.choices[0].message.content\n",
    "                                           "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5e1a41a-b03c-4408-a0f5-00529785f3d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def claude_password_gen(required_len, spec_char, num_char,min_lowercase,min_uppercase):\n",
    "    response = claude.messages.create(\n",
    "        model=CLAUDE_MODEL,\n",
    "        max_tokens=max_tok,\n",
    "        system=pass_system_prompt(required_len, spec_char, num_char,min_lowercase,min_uppercase),\n",
    "        messages = [{\"role\":\"user\",\"content\":pass_user_prompt(required_len, spec_char, num_char,min_lowercase,min_uppercase)}]\n",
    "    )\n",
    "    return response.content[0].text\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a41a0a2-55a1-47e5-8fc0-5dd04ebd3573",
   "metadata": {},
   "outputs": [],
   "source": [
    "def google_password_gen(required_len, spec_char, num_char,min_lowercase,min_uppercase):\n",
    "    message = google.generativeai.GenerativeModel(\n",
    "        model_name=GOOGLE_MODEL,\n",
    "        system_instruction=pass_system_prompt(required_len, spec_char, num_char,min_lowercase,min_uppercase)\n",
    "    )\n",
    "    response = message.generate_content(pass_user_prompt(required_len, spec_char, num_char,min_lowercase,min_uppercase))\n",
    "    return response.text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dcd1ce50-6576-4594-8739-1d7daf602213",
   "metadata": {},
   "outputs": [],
   "source": [
    "#test\n",
    "messages1 = openai_password_gen(12, \"N\",\"Y\",1,1)\n",
    "messages2 = claude_password_gen(12,\"N\",\"Y\",1,1)\n",
    "messages3= google_password_gen(12,\"N\",\"Y\",1,1)\n",
    "print(\"OpenAI: \",messages1)\n",
    "print(\"Claude: \", messages2)\n",
    "print(\"Gemini: \", messages3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cec429a-2355-4941-8422-480b2614009c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# model select\n",
    "\n",
    "def select_model(required_len, spec_char, num_char,min_lowercase,min_uppercase,model):\n",
    "    if model == \"OpenAI\":\n",
    "        return openai_password_gen(required_len, spec_char, num_char,min_lowercase,min_uppercase)\n",
    "    elif model == \"Claude\":\n",
    "        return claude_password_gen(required_len, spec_char, num_char,min_lowercase,min_uppercase)\n",
    "    elif model == \"Gemini\":\n",
    "        return google_password_gen(required_len, spec_char, num_char,min_lowercase,min_uppercase)\n",
    "    else:\n",
    "        print(\"No model selected\")\n",
    "        return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bef52e6d-dc50-4c91-9d56-624dfdd66276",
   "metadata": {},
   "outputs": [],
   "source": [
    "test = select_model(12, \"N\",\"Y\",1,1,\"OpenAI\")\n",
    "\n",
    "print(test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b9d3685-a1b8-470c-8f4b-e63d68a0240d",
   "metadata": {},
   "outputs": [],
   "source": [
    "css = \"\"\"\n",
    "#password_box textarea {\n",
    "    background-color: #306998;\n",
    "    color: white;\n",
    "}\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81c423ec-0ca7-4c96-a2fe-02ed2b5f3839",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "with gr.Blocks(css=css) as demo:\n",
    "    gr.Markdown(\"Choose your password complexity requirements and run:\")\n",
    "    with gr.Row():\n",
    "        with gr.Column(min_width=150,scale=2):\n",
    "            with gr.Row():\n",
    "                required_len = gr.Number(label=\"Specify the required length\",value=12,minimum=1,maximum=30)\n",
    "                min_lowercase = gr.Number(label=\"the minimum lowercase letters\", value=1,minimum=0)\n",
    "                min_uppercase = gr.Number(label=\"the minimum uppercase letters\", value=1,minimum=0)\n",
    "        with gr.Column():\n",
    "            spec_char = gr.Checkbox(label=\"Include special characters?\",value=True)\n",
    "            num_char = gr.Checkbox(label=\"Include numbers?\", value=True)\n",
    "    with gr.Row():\n",
    "        with gr.Column():\n",
    "            model = gr.Dropdown([\"OpenAI\",\"Claude\",\"Gemini\"])\n",
    "            btn = gr.Button(\"Run\")\n",
    "        with gr.Column():\n",
    "            output = gr.Textbox(label=\"Password:\", elem_id=\"password_box\")\n",
    "    \n",
    "    btn.click(fn=select_model,inputs=[required_len,spec_char,num_char,min_lowercase,min_uppercase,model],outputs=output)\n",
    "\n",
    "demo.launch()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d81a8318-57ef-46ae-91b7-ae63d661edd8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
